﻿using Xejen.Communication.Configuration;
using Xejen.Communication.Http.Abstract;
using Xejen.Communication.Http.Communicator;
using Xejen.Communication.Http.Configuration;
using Xejen.Hosting;
using Xejen.Hosting.Extensions;
using Xejen.Logger;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Text;

namespace Xejen.Communication.Http.Extensions
{
    /// <summary>
    /// <inheritdoc cref="IHttpCommunicatorBuilder"/>的实现
    /// </summary>
    internal sealed class HttpCommunicatorBuilder : CommunicatorBuilder<IHttpCommunicatorBuilder>, IHttpCommunicatorBuilder
    {
        private readonly ILogger _logger;
        private readonly HttpConfig _httpConfig = new HttpConfig();

        /// <inheritdoc cref="HttpCommunicatorBuilder"/>
        /// <param name="host"><inheritdoc cref="IAppHost" path="/summary"/></param>
        public HttpCommunicatorBuilder(IAppHost host) : base(host)
        {
            _logger = LoggerManager.CreateLogger<HttpCommunicatorBuilder>();
        }

        /// <inheritdoc/>
        public IHttpCommunicatorBuilder ConfigureHttpConfiguration(Action<HttpConfig> configure)
        {
            Configuration.Bind(nameof(HttpConfig), _httpConfig);

            configure?.Invoke(_httpConfig);

            this.ConfigureServices((services) =>
            {
                services.AddSingleton(_httpConfig);
            });

            _logger.LogInformation($"完成{nameof(HttpConfig)}配置：{{@value}}", _httpConfig);

            return this;
        }
    }
}
